package org.amj.wn.sect;

import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

public class SectWaqfeNau {
	
	public enum SectProperty {
		memberCode, passCode, jamaat, name
	}
	
	private static final String ENTITY_NAME = "SectWaqfeNou";
	private static final String PARENT_ENTITY_NAME = "AMJCenter";
	private final Entity entity;
	

	public SectWaqfeNau(Entity result) {
		this.entity = result;
	}

	public Object getProperty(SectProperty property) {
		return entity.getProperty(property.toString());
	}

	public void setProperty(SectProperty property, Object value) {
		entity.setProperty(property.toString(), value);
	}


	public Entity getEntity() {
		return entity;
	}

	public static SectWaqfeNau get(String memberCode) {

		SectWaqfeNau wn = null;

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Filter memberCodeFilter = new FilterPredicate(SectProperty.memberCode.toString(),
				FilterOperator.EQUAL, memberCode);

		Query q = new Query(ENTITY_NAME, parentKey()).setFilter(memberCodeFilter);

		PreparedQuery pq = datastore.prepare(q);

		List<Entity> resultList = pq.asList(FetchOptions.Builder.withLimit(1));

		Entity result = null;

		if(resultList != null && resultList.size() > 0) {
			result = resultList.get(0);
			wn = new SectWaqfeNau(result);
		} else {
			wn = null;
		}

		return wn;
	}



	public static boolean createOrUpdate(String memberCode, String passCode, String name, String jamaat) {

		SectWaqfeNau waqfeNau = get(memberCode);


		if(waqfeNau == null) {
			waqfeNau = new SectWaqfeNau(new Entity(ENTITY_NAME, parentKey()));
		}

		if(jamaat == null || "".equals(jamaat))
			return false;

		waqfeNau.setProperty(SectProperty.memberCode, memberCode);
		waqfeNau.setProperty(SectProperty.passCode, passCode);
		waqfeNau.setProperty(SectProperty.name, name);
		waqfeNau.setProperty(SectProperty.jamaat, jamaat);

		return waqfeNau.save();
	}

	public static boolean modify(String memberCode, String passCode,
			String name, String jamaat) {

		SectWaqfeNau waqfeNau = get(memberCode);
		if(waqfeNau == null)
			return false;
		if(jamaat == null || "".equals(jamaat))
			return false;

		waqfeNau.setProperty(SectProperty.passCode, passCode);
		waqfeNau.setProperty(SectProperty.name, name);
		waqfeNau.setProperty(SectProperty.jamaat, jamaat);
		return waqfeNau.save();

	}

	private boolean save() {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

		datastore.put(entity);
		return true;
	}

	public static void delete(String memberCode) {
		SectWaqfeNau waqfeNau = get(memberCode);
		waqfeNau.delete();

	}
	
	private static Key parentKey() {
		return KeyFactory.createKey(PARENT_ENTITY_NAME, "sectwaqfeen");
	}

	private void delete() {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.delete(entity.getKey());
	}

	public String getPassCode() {
		Object name = getProperty(SectProperty.passCode);
		return name == null ? "" : name.toString();
	}

	public String getMemberCode() {
		Object name = getProperty(SectProperty.memberCode);
		return name == null ? "" : name.toString();
	}

	public String getName() {
		Object name = getProperty(SectProperty.name);
		return name == null ? "" : name.toString();
	}
	
	public String getJamaat() {
		Object name = getProperty(SectProperty.jamaat);
		return name == null ? "" : name.toString();
	}
	
	public static List<Entity> search(int startIndex, int pageSize) {
	    Query query = new Query(ENTITY_NAME, parentKey());
	    
    	query.addSort(SectProperty.memberCode.toString(), Query.SortDirection.ASCENDING);
		
	    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    return datastore.prepare(query).asList(FetchOptions.Builder.withLimit(pageSize).offset(startIndex));

	}
}
